home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / board / Crafty-15.19.lha / crafty-15.19 / src / valid.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  7KB  |  180 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "chess.h"
  4. #include "data.h"
  5.  
  6. /* last modified 02/27/97 */
  7. /*
  8. ********************************************************************************
  9. *                                                                              *
  10. *   ValidMove() is used to verify that a move is valid.  it is primarily used  *
  11. *   to confirm that a move retrieved from the transposition/refutation table   *
  12. *   is valid in the current position by checking the move against the current  *
  13. *   chess board, castling status, en passant status, etc.                      *
  14. *                                                                              *
  15. ********************************************************************************
  16. */
  17. int ValidMove(TREE *tree, int ply, int wtm, int move)
  18. {
  19. /*
  20.  ----------------------------------------------------------
  21. |                                                          |
  22. |   make sure that the piece on <from> is the right color. |
  23. |                                                          |
  24.  ----------------------------------------------------------
  25. */
  26.   if (wtm) {
  27.     if (PieceOnSquare(From(move)) != Piece(move)) return(0);
  28.   }
  29.   else {
  30.     if (PieceOnSquare(From(move)) != -Piece(move)) return(0);
  31.   }
  32.       
  33.   switch (Piece(move)) {
  34.     case empty:
  35.       return(0);
  36. /*
  37.  ----------------------------------------------------------
  38. |                                                          |
  39. |   king moves are validated here if the king is moving    |
  40. |   two squares at ont time (castling moves).  otherwise   |
  41. |   fall into the normal piece validation routine below.   |
  42. |   for castling moves, we need to verify that the         |
  43. |   castling status is correct to avoid "creating" a new   |
  44. |   rook.                                                  |
  45. |                                                          |
  46.  ----------------------------------------------------------
  47. */
  48.     case king:
  49.       if (abs(From(move)-To(move)) == 2) {
  50.         if (wtm) {
  51.           if (WhiteCastle(ply) > 0) {
  52.             if (To(move) == 2) {
  53.               if ((!(WhiteCastle(ply)&2)) ||
  54.                   And(Occupied,Shiftr(mask_3,1)) ||
  55.                   And(AttacksTo(tree,2),BlackPieces) ||
  56.                   And(AttacksTo(tree,3),BlackPieces) ||
  57.                   And(AttacksTo(tree,4),BlackPieces)) return(0);
  58.             }
  59.             else if (To(move) == 6) {
  60.               if ((!(WhiteCastle(ply)&1)) ||
  61.                   And(Occupied,Shiftr(mask_2,5)) ||
  62.                   And(AttacksTo(tree,4),BlackPieces) ||
  63.                   And(AttacksTo(tree,5),BlackPieces) ||
  64.                   And(AttacksTo(tree,6),BlackPieces)) return(0);
  65.             }
  66.           }
  67.           else return(0);
  68.         }
  69.         else {
  70.           if (BlackCastle(ply) > 0) {
  71.             if (To(move) == 58) {
  72.               if ((!(BlackCastle(ply)&2)) ||
  73.                   And(Occupied,Shiftr(mask_3,57)) ||
  74.                   And(AttacksTo(tree,58),WhitePieces) ||
  75.                   And(AttacksTo(tree,59),WhitePieces) ||
  76.                   And(AttacksTo(tree,60),WhitePieces)) return(0);
  77.             }
  78.             if (To(move) == 62) {
  79.               if ((!(BlackCastle(ply)&1)) ||
  80.                   And(Occupied,Shiftr(mask_2,61)) ||
  81.                   And(AttacksTo(tree,60),WhitePieces) ||
  82.                   And(AttacksTo(tree,61),WhitePieces) ||
  83.                   And(AttacksTo(tree,62),WhitePieces)) return(0);
  84.             }
  85.           }
  86.           else return(0);
  87.         }
  88.         return(1);
  89.       }
  90.       break;
  91. /*
  92.  ----------------------------------------------------------
  93. |                                                          |
  94. |   check for a normal pawn advance.                       |
  95. |                                                          |
  96.  ----------------------------------------------------------
  97. */
  98.     case pawn:
  99.       if (abs(From(move)-To(move)) == 8) {
  100.         if (wtm) {
  101.           if ((Piece(move) == PieceOnSquare(From(move))) &&
  102.               (From(move) < To(move)) && !PieceOnSquare(To(move))) return(1);
  103.         }
  104.         else {
  105.           if ((Piece(move) == -PieceOnSquare(From(move))) &&
  106.               (From(move) > To(move)) && !PieceOnSquare(To(move))) return(1);
  107.         }
  108.         return(0);
  109.       }
  110.       else if (abs(From(move)-To(move)) == 16) {
  111.         if (wtm) {
  112.           if ((Piece(move) == PieceOnSquare(From(move))) &&
  113.               !PieceOnSquare(To(move)-8) && !PieceOnSquare(To(move))) return(1);
  114.         }
  115.         else {
  116.           if ((Piece(move) == -PieceOnSquare(From(move))) &&
  117.               !PieceOnSquare(To(move)+8) && !PieceOnSquare(To(move))) return(1);
  118.         }
  119.         return(0);
  120.       }
  121.       if (!Captured(move)) return(0);
  122. /*
  123.  ----------------------------------------------------------
  124. |                                                          |
  125. |   check for an en passant capture which is somewhat      |
  126. |   unusual in that the [to] square does not contain the   |
  127. |   pawn being captured.  make sure that the pawn being    |
  128. |   captured advanced two ranks the previous move.         |
  129. |                                                          |
  130.  ----------------------------------------------------------
  131. */
  132.       if (wtm) {
  133.         if ((PieceOnSquare(From(move)) == pawn) &&
  134.             (PieceOnSquare(To(move)) == 0) &&
  135.             (PieceOnSquare(To(move)-8) == -pawn) &&
  136.             And(EnPassantTarget(ply),set_mask[To(move)])) return(1);
  137.       }
  138.       else {
  139.         if ((PieceOnSquare(From(move)) == -pawn) &&
  140.             (PieceOnSquare(To(move)) == 0) &&
  141.             (PieceOnSquare(To(move)+8) == pawn) &&
  142.             And(EnPassantTarget(ply),set_mask[To(move)])) return(1);
  143.       }
  144. /*
  145.  ----------------------------------------------------------
  146. |                                                          |
  147. |   normal moves are all checked the same way.             |
  148. |                                                          |
  149.  ----------------------------------------------------------
  150. */
  151.     case queen:
  152.     case rook:
  153.     case bishop:
  154.     case knight:
  155.       break;
  156.   }
  157. /*
  158.  ----------------------------------------------------------
  159. |                                                          |
  160. |   all normal moves are validated in the same manner, by  |
  161. |   checking the from and to squares and also the attack   |
  162. |   status for completeness.                               |
  163. |                                                          |
  164.  ----------------------------------------------------------
  165. */
  166.   if (wtm) {
  167.     if (Piece(move) == PieceOnSquare(From(move)) &&
  168.         Captured(move) == -PieceOnSquare(To(move)) &&
  169.         Captured(move) != -king &&
  170.         And(AttacksFrom(tree,From(move),wtm),set_mask[To(move)])) return(1);
  171.   }
  172.   else {
  173.     if (Piece(move) == -PieceOnSquare(From(move)) &&
  174.         Captured(move) == PieceOnSquare(To(move)) &&
  175.         Captured(move) != king &&
  176.         And(AttacksFrom(tree,From(move),wtm),set_mask[To(move)])) return(1);
  177.   }
  178.   return(0);
  179. }
  180.